package com.ys.controller.api.mall;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bo.vo.R;
import com.ys.entity.mall.TmGoods;
import com.ys.service.mall.TmGoodsService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/api/inter/tmGoods")
public class ApiInterTmGoodsController {
	@Autowired
	private CommonApiService<TmGoods> commonApiService;
	@Autowired
	private TmGoodsService tmGoodsService;

	@RequiresPermissions("tmGoods:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TmGoods cond){
		cond.setOrderby("A.SortNo asc");
		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/list")
	@ResponseBody
	public R list(String content, Long cateId, Integer page){
		System.out.println("content---" + content);
		System.out.println("page---" + page);
		page = page == null ? 0 : page - 1;
		
		TmGoods cond = new TmGoods()
				.setCateId(cateId)
				.setPage(page)
				.setSize(10)
				.setOrderby("A.SortNo asc");
		if (content != null && content.length() > 0) {
			cond.where("A.Name like '%" + content + "%'");
		}
		List<ListDto.RowDto> rows = new ArrayList<ListDto.RowDto>();
		List<TmGoods> listEntity = cond.queryList();
		for(TmGoods entity : listEntity) {
			Long id = entity.getId();
			String code = entity.getCode();
			String name = entity.getName();
			String img = entity.getImg();
			BigDecimal amount = entity.getAmount();
			Integer stockNum = entity.getStockNum();
			rows.add(new ListDto.RowDto(id, name, code, img, amount, stockNum));
		}
		
		Integer totalNum = null;
		if (page == 0) {
			totalNum = cond.count();
		}
		ListDto dto = new ListDto(rows, totalNum);
		return R.ok(dto);
	}
	@Data
	@AllArgsConstructor
	public static class ListDto{
		private List<RowDto> rows; // 数据行
		private Integer totalNum; // 总条数
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private Long id; // 字段ID
			private String name; // 字段名称
			private String code; // 编码
			private String img; // 封面图
			private BigDecimal amount; // 售价
			private Integer stockNum; // 库存
		}
	}
	
	
	/**
	 * 新增
	 */
	@RequiresPermissions("tmGoods:add")
	@PostMapping("/add")
    @ResponseBody
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params){

		return tmGoodsService.add(params);
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/edit")
    @ResponseBody
	public R edit(@RequestParam Map<String, Object> params, Long id, Integer numChange){
		try {
			return tmGoodsService.edit(params, id, numChange);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}

	}
	
	/**
	 * 置顶
	 */
	@PostMapping("/moveTop")
    @ResponseBody
	public R moveTop(Long id){
		try {
			return tmGoodsService.moveTop(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}

	}
	
	/**
	 * 调整顺序
	 */
	@RequiresPermissions("tmGoods:resort")
	@PostMapping("/resort")
    @ResponseBody
	public R resort(String rows){
		return tmGoodsService.resort(rows);
	}
	
	/**
	 * 置顶
	 */
	@PostMapping("/detailGoods")
    @ResponseBody
	public R detailGoods(String code){
		
		TmGoods entity = new TmGoods().setCode(code).queryFirst();
		if (entity == null) {
			return R.error("商品不存在");
		}
		
		Long id = entity.getId();
		String name = entity.getName();
		String img = entity.getImg();
		BigDecimal salePrice = entity.getAmount();
		Integer stockNum = entity.getStockNum();
		Integer saleNum = 0;
		String detail = entity.getDetail();
		DetailGoodsDto dto = new DetailGoodsDto(id, name, code, img, salePrice, stockNum, saleNum, detail);
		return R.ok(dto);
	}
	@Data
	@AllArgsConstructor
	public static class DetailGoodsDto{
		private Long id; // 字段ID
		private String name; // 字段名称
		private String code; // 编码
		private String img; // 封面图
		private BigDecimal salePrice; // 售价
		private Integer stockNum; // 库存
		private Integer saleNum; // 销量
		private String detail; // 商品详情，富文本
	}
}